package com.pomr.controller.sys;

import com.pomr.entity.EffectEntity;
import com.pomr.entity.RoleParam;
import com.pomr.model.sys.Effect;
import com.pomr.model.sys.Role;
import com.pomr.service.sys.RoleService;
import com.pomr.util.common.CommonData;
import com.pomr.util.common.ResponseData;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @作者：Administrator
 * @时间：1:31 2018/9/6 0006
 * @用途：角色管理
 **/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    @Qualifier("roleServiceImpl")
    private RoleService roleService;

    @RequestMapping(value="/inter-searchAll", method = RequestMethod.GET)
    public Page<List<Role>> searchAllRoles(PageParam pageParam) throws Exception{
        return roleService.searchAllRoles(pageParam);
    }
    @RequestMapping(value="/inter-searchLikeName", method = RequestMethod.GET)
    public Page<List<Role>> searchRolesLikeName(PageParam pageParam) throws Exception{
        return roleService.searchRolesLikeName(pageParam);
    }
    @RequestMapping(value="/inter-searchEffects", method = RequestMethod.POST)
    public ResponseData searchEffects() throws Exception{
        ResponseData pd = ResponseData.ok();
        pd.putDataValue("effects", EffectEntity.instanceEffect(roleService.searchEffects()));
        return pd;
    }
    @RequestMapping(value="/inter-insert", method = RequestMethod.POST)
    public ResponseData insertRole(@RequestBody RoleParam role) throws Exception{
        ResponseData pd = ResponseData.ok();
        if(!roleService.insertRole(role)){
            pd = ResponseData.custom("保存角色信息失败");
        }
        return pd;
    }
    @RequestMapping(value="/inter-delete", method = RequestMethod.POST)
    public ResponseData deleteRoles(@RequestBody Map<String, String> map) throws Exception{
        ResponseData pd = ResponseData.ok();
        String ids = map.get("ids");
        if(!roleService.deleteRoles(ids.split(","))){
            pd = ResponseData.custom("删除角色信息失败");
        }
        return pd;
    }

    /**
     * 通过id获取角色信息
     * @param roleid
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/inter-searchRoleById", method = RequestMethod.GET)
    public ResponseData searchRoleById(String roleid) throws Exception{
        return roleService.seachRoleById(roleid);
    }

    @RequestMapping(value="/inter-edit", method = RequestMethod.POST)
    public ResponseData editRole(@RequestBody RoleParam role) throws Exception{
        ResponseData pd = ResponseData.ok();
        return roleService.editRole(role);
    }

}
